<?php
/**
 * Menu(企业管理)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ComController extends AdminbaseController
{
    protected $coms_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->coms_model = D("Common/Coms");
    }

    // 后台企业列表
    public function index()
    {
        $where = array("com_state"=>1);
        $com_name = I('request.com_name');
        $province =  I('request.province');
        $city =  I('request.city');
        $district =  I('request.district');
        if($com_name){
            $where['com_name'] = array('like',"%$com_name%");
        }
        if($province){
            $where['province'] =$province;
            $this->assign('province',$province);
            if($city){
                $where['city'] =$city;
                $this->assign('city',$city);
                if($district){
                    $where['district'] =$district;
                    $this->assign('district',$district);
                }
            }
        }
        $count = $this->coms_model->where($where)->count();
        $page = $this->page($count, 20);
        $list = $this->coms_model->order("created_time DESC")
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    // 企业新增页面
    public function add()
    {
        parent::dict_select(array(array(
            'code' => 'COM_PJXZ',
            'name' => 'pay',
            'text' => '平均薪资'
        ), array(
            'code' => 'COM_SSHT',
            'name' => 'trade',
            'text' => '所属行业'
        ), array(
            'code' => 'COM_XZ',
            'name' => 'nature',
            'text' => '企业性质'
        ), array(
            'code' => 'COM_GM',
            'name' => 'scale',
            'text' => '企业规模'
        )
        ));
        $this->display();
    }

    //企业新增数据提交
    public function add_post()
    {
        if (IS_POST) {
            $_POST['created_time'] = time();
            if ($this->coms_model->create() !== false) {
                $com_objct = $_POST;
                if ($this->coms_model->add($com_objct) !== false) {
                    $this->success("添加企业成功", U("com/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->coms_model->getError());
            }
        }
    }

    /**
     * 跳转到修改页面
     */
    public function edit()
    {
        $id = I("get.id", 0, 'intval');
        $rs = $this->coms_model->where(array("id" => $id))->find();
        parent::dict_select(array(array(
            'code' => 'COM_PJXZ',
            'name' => 'pay',
            'text' => '平均薪资',
            'value' => $rs['pay']
        ), array(
            'code' => 'COM_SSHT',
            'name' => 'trade',
            'text' => '所属行业',
            'value' => $rs['trade']
        ), array(
            'code' => 'COM_XZ',
            'name' => 'nature',
            'text' => '企业性质',
            'value' => $rs['nature']
        ), array(
            'code' => 'COM_GM',
            'name' => 'scale',
            'text' => '企业规模',
            'value' => $rs['scale']
        )
        ));
        //行政区单独处理
        $this->assign('province',$rs['province']);
        $this->assign('city',$rs['city']);
        $this->assign('district',$rs['district']);
        $this->assign('data',$rs);
        $this->display();
    }

    function edit_post(){
        if (IS_POST) {
            if ($this->coms_model->create()!==false) {
                $com_objct = $_POST;
                $com_objct['updatad_time'] = time();
                $result=$this->coms_model->save($com_objct);
                if ($result !== false) {
                    $this->success("修改企业成功", U("com/index"));
                }else{
                    $this->error("更新失败！");
                }
            }else{
                $this->error($this->coms_model->getError());
            }

        }
    }

    /**
     * 删除企业
     */
    public function delete() {
        $id = I("get.id",0,'intval');
        $delete_state = C('COM_STATE')[2]['dict_value'];
        $this->coms_model->com_state = $delete_state;
        $this->coms_model->deleted_time = time();
        $rs= $this->coms_model->where('id='.$id)->save(); // 根据条件更新记录
       if ($rs!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
    
    /**
     * @info 企业置顶显示
     * @author Mojianguo
     * @date 2017年2月10日 17:55:23
     */
    public function is_top() {
    	$id = I("get.id",0,'intval');
    	$is_top = I("get.is_top",0,'intval');
    	$this->coms_model->is_top = $is_top;
    	$rs= $this->coms_model->where('id='.$id)->save(); // 根据条件更新记录
    	if ($rs!==false) {
    		$this->success("操作成功！");
    	} else {
    		$this->error("操作失败！");
    	}
    }
    
    /**
     * @info 企业停招
     * @author Mojianguo
     * @date 2017年2月21日 10:27:23
     */
    public function is_nojob() {
    	$id = I("get.id",0,'intval');
    	$is_nojob = I("get.is_nojob",0,'intval');
    	$this->coms_model->is_nojob = $is_nojob;
    	$rs= $this->coms_model->where('id='.$id)->save(); // 根据条件更新记录
    	if ($rs!==false) {
    		$this->success("操作成功！");
    	} else {
    		$this->error("操作失败！");
    	}
    }
}
